
package demo1;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Demo1 {

    // Globals.
    static PTimer pt;
    static long frameTime;
    static Starfield sf;
    static double fovX;
    static double fovY;
    
    // Create a class for our double-buffered Canvas.
    public static class DBuffer extends DoubleBuffer {
        // Constructor.
        public DBuffer() {
            super();
        }

        // Call this to update the front buffer.
        public void doUpdate() {
            repaint();
        }
        
        // Draw to back-buffer.
        @Override
        public void paintBuffer(Graphics g) {
            // Call our drawing functions here!
            sf.draw(g, 800, 600, fovX, fovY);
        }
    }
    
    // Program entry point. MAIN
    public static void main(String[] args) {
        // Create our main frame and double-buffered canvas.
        JFrame frame = new JFrame();
        DBuffer db = new DBuffer();
        
        // Set attributes of the frame.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocation(16, 16);
        frame.setResizable(false);
        frame.setTitle("Demo 1");
        
        // Set the canvas size to 800x600 pixels with black bg.
        db.setSize(800, 600);
        db.setBackground(Color.black);
        
        // Add the double-buffered Canvas to the frame and show it.
        frame.add(db);
        frame.pack();
        frame.setVisible(true);
        
        // Create the starfield.
        sf = new Starfield(3000, 3000, 3000, 5000);
        fovX = 1.0;
        fovY = 800.0 / 600.0;
        
        // Create a frame timer.
        pt = new PTimer();
        
        double speed = 0.0001;
        int speedTime = 100;
        
        // Main Loop
        while(true) {
            frameTime = pt.update();
            sf.moveStarfield(0.0, 0.0, -speed * frameTime);
            speed += 0.0001 * frameTime;
            db.doUpdate();
            speedTime--;
            if( speedTime <= 0 ) {
                speedTime += 100;
                speed -= 0.00001;
            }
        }
    }
}
